/* Copyright 2009 Sony Mathew, Minneapolis MN. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at  http://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */
package com.xsm.servlet;

import java.net.MalformedURLException;

import javax.servlet.http.HttpServletRequest;

import com.xsm.lite.util.StringUtil;

/**
 * Provides simple utilities for a Servlet or Filter.
 * 
 * @author Sony Mathew
 */
public class ServletUtil {

	/**
	 * Given an HTTP Request URL - obtains the uri portion of the path making this request.
	 * 	It gives everything after the contextRoot including the first '/' after contextRoot if it exists.
	 * 
	 * if URL is http:/myhost:port/contextRoot/mainPath/extra1/extra2/
	 * 		will return /mainPath/extra1/extra2/ including the first '/' after contextRoot if it exists. 
	 * 
	 * Note: Probably could rely on servletPath and PathInfo - but they are inconsisted between spec versions. 
	 * 	so relying on HttpServletRequest.getRequestURI() - which is still consistent.
	 * 
	 * author Sony Mathew
	 */
	public static String getRequestPath(HttpServletRequest httpRequest) throws MalformedURLException {
		String uri = httpRequest.getRequestURI();
		
		if (uri == null || (uri=uri.trim()).length()==0) {
			throw new MalformedURLException(uri);			
		}
		
		if (! uri.startsWith(httpRequest.getContextPath())) {
			throw new MalformedURLException(uri);
		}
		
		return uri.substring(httpRequest.getContextPath().length()); 		
	}

	/**
	 * Tokenizes a requestPath
	 * 
	 * @see #getRequestPath(HttpServletRequest)
	 * 
	 * author Sony Mathew
	 */
	public static String[] getTokenizedRequestPath(HttpServletRequest httpRequest) throws MalformedURLException {
		return StringUtil.pathSplit(getRequestPath(httpRequest));		
	}
	
	/**
	 * Tokenizes a requestPath and gives the mainPath and the extraPaths.
	 * 
	 * @see ServletUtil.getRequestPath(HttpServletRequest).
	 * 
	 * @author Sony Mathew
	 */
	public static class RequestPathInfo {
		public String mainPath = "";
		public String[] extraPaths = new String[0];
	}
	
	/**
	 * Tokenizes a requestPath into a RequestInfo object.
	 * 
	 * @see #getRequestPath(HttpServletRequest)
	 * 
	 * author Sony Mathew
	 */
	public static RequestPathInfo getRequestPathInfo(HttpServletRequest req) throws MalformedURLException {
		RequestPathInfo pi = new RequestPathInfo();
		
		String[] requestTokens = ServletUtil.getTokenizedRequestPath(req);
		
		if (requestTokens != null && requestTokens.length > 0) {
			pi.mainPath = requestTokens[0];
			if (requestTokens.length > 1) {
				pi.extraPaths = new String[requestTokens.length - 1];
				System.arraycopy(requestTokens, 1, pi.extraPaths, 0, pi.extraPaths.length);			
			}
		}
		
		return pi;
	}
	
}
